package com.weaving.monitor.service;

import com.weaving.monitor.dto.MachineStatusDto;
import com.weaving.monitor.entity.Machine;
import com.weaving.monitor.repository.MachineRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class MachineService {
    
    private final MachineRepository machineRepository;
    
    public List<MachineStatusDto> getAllMachineStatus() {
        List<Machine> machines = machineRepository.findAll();
        return machines.stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    public MachineStatusDto getMachineStatus(Integer machineNumber) {
        Machine machine = machineRepository.findByMachineNumber(machineNumber);
        return machine != null ? convertToDto(machine) : null;
    }
    
    private MachineStatusDto convertToDto(Machine machine) {
        MachineStatusDto dto = new MachineStatusDto();
        dto.setMachineNumber(machine.getMachineNumber());
        dto.setModel(machine.getModel());
        dto.setFabricLength(machine.getFabricLength());
        dto.setOperator(machine.getOperator());
        dto.setSpeed(machine.getSpeed());
        dto.setL1Pattern(machine.getL1Pattern());
        dto.setL1Remaining(machine.getL1Remaining());
        dto.setL2Pattern(machine.getL2Pattern());
        dto.setL2Remaining(machine.getL2Remaining());
        dto.setL3Pattern(machine.getL3Pattern());
        dto.setL3Remaining(machine.getL3Remaining());
        dto.setL4Pattern(machine.getL4Pattern());
        dto.setL4Remaining(machine.getL4Remaining());
        dto.setNextPlan(machine.getNextPlan());
        dto.setStatus(machine.getStatus());
        dto.setLastUpdated(machine.getLastUpdated());
        return dto;
    }
} 